/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.ac.iie.di.splitor;

import cn.ac.iie.di.global.GlobalParas;
import cn.ac.iie.di.tools.FileTools;
import cn.ac.iie.di.untar.TarTask;
import java.io.File;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.TimeUnit;
import org.apache.log4j.Logger;

/**
 *
 * @author lenovo-pc
 */
public class UntarSplitor implements Runnable {

	String dir = null;
	//String desdir = null;
	String FileString = null;

	static Logger logger = null;

	static {
		logger = org.apache.log4j.Logger.getLogger(UntarSplitor.class.getName());
	}

	public UntarSplitor(String dir, String filetype) {
		this.dir = dir;
		//this.desdir = desdir;
		this.FileString = filetype;
	}

	@Override
	public void run() {
		FileTools ft = new FileTools();
		while (true) {
			try {
				logger.info("the original dir is " + dir);
				List<File> gzFiles = ft.getFiles1(new File(dir), "sig");
				HashSet<File> gzSet = new HashSet<>(gzFiles);
				logger.info("================================the Filelist size " + gzSet.size());
				gzFiles.clear();
				gzFiles.addAll(gzSet);
				logger.info("current sig files " + gzFiles);
				//for (File gzFile : gzFiles) {
				for (int i = 0; i < gzFiles.size(); i++) {
					File gzFile = gzFiles.get(i);
					String gzFileName = gzFile.getName().split(".sig")[0];
					//文件不存在.ok放入队列
					File fileok = new File(GlobalParas.ORI_GWX+File.separator+gzFileName.split(".sig")[0] + ".ok");
					File filelock = new File(GlobalParas.ORI_GWX+File.separator+gzFileName + ".sig.lock");
					if (!filelock.exists()) {
						if (!fileok.exists()) {
							GlobalParas.untarQueue.offer(gzFileName);
							FileTools.creatTxtFile(GlobalParas.ORI_GWX+File.separator+gzFileName + ".sig.lock");
						}
					}
					logger.info("file:" + gzFileName);
				}
				logger.debug("GlobalParas.ungzipQueue.size(): " + GlobalParas.untarQueue.size());
				Iterator it = GlobalParas.untarQueue.iterator();
				while (it.hasNext()) {
					logger.debug("==========" + it.next().toString());
				}
				Thread.sleep(3000);
			} catch (Exception ex) {
				logger.error("===========================The uncompressed error:" + ex, ex);
			}

		}
	}

	public static void main(String[] args) {
		File f = new File("D:\\123\\pa_basic_tran\\pa_basic");
		System.out.println(f.getName());
	}

}
